<?php

/**
 *
 * @author Administrator
 */
class ContentController {
	// TODO - Insert your code here
	private $path;
	private $lan;

	/**
	 */
	function __construct() {

		$this->path = "src/module/pc_view/content/temp/";
		if (isset( $_REQUEST[PARAM_LAN])) {
			$this->lan = $_REQUEST[PARAM_LAN];
		} else {
			$this->lan = DEFAULT_LANGAUGE;
		}
	}

	/**
	 *
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewHomePage() {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/homepage.html");
		
		// return page
		$template->parse( PAGECONTENT);
		return array(WEBHEADER => null, PAGECONTENT => $template->out( PAGECONTENT));
	}

	/**
	 *
	 * @param unknown_type $type
	 */
	public function viewListCate($type = CATEGORY_TYPE_PRODUCT) {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/listcate.html");
		$db = new DBAccess();
		
		$list = $db->selectManyByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_TYPE => $type));
		
		foreach ( $list as $item ) {
			
			$item[CATEGORY_URL] = createURL( $this->lan, CATEGORIES, $item[CATEGORY_URL]);
			$template->assign( ITEM, $item);
			$template->parse( PAGECONTENT . "." . ITEM);
		}
		
		// return page
		$template->parse( PAGECONTENT);
		return array(WEBHEADER => null, PAGECONTENT => $template->out( PAGECONTENT));
	}

	/**
	 *
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewListProduct($cate = null, $type = CATEGORY_TYPE_DOMESTIC) {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/listproduct.html");
		$db = new DBAccess();
		
		$list = $db->selectManyByCause( $GLOBALS['productFullColArray'], PRODUCTS, array(PRODUCT_CATEGORYID => $cate[CATEGORY_ID]));
		
		foreach ( $list as $item ) {
			$item[PRODUCT_PRICE] = editPrice( $item[PRODUCT_PRICE], $item[PRODUCT_CURRENCY], 1);
			$item[PRODUCT_URL] = createURL( $this->lan, TOURS, $item[PRODUCT_URL]);
			
			$template->assign( ITEM, $item);
			$template->parse( PAGECONTENT . "." . ITEM);
		}
		
		$template->assign( PATHWAY, createPathWay( $cate[CATEGORY_ID]));
		
		// return page
		$template->parse( PAGECONTENT);
		return array(WEBHEADER => $cate, PAGECONTENT => $template->out( PAGECONTENT));
	}

	/**
	 *
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewDetailProduct() {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/detailproduct.html");
		
		// product detail
		$db = new DBAccess();
		$detail = $db->selectOneByCause( $GLOBALS['productFullColArray'], PRODUCTS, array(PRODUCT_URL => $_REQUEST[PARAM_URL]));
		
		$detail[PRODUCT_SHOWPRICE] = editPrice( $detail[PRODUCT_SHOWPRICE], $detail[PRODUCT_SHOWCURRENCY]);
		$detail[PRODUCT_PRICE] = editPrice( $detail[PRODUCT_PRICE], $detail[PRODUCT_CURRENCY], 0);
		$detail[PRODUCT_URL] = createURL( $this->lan, PRODUCTS, $detail[PRODUCT_URL]);
		
		// product image slide box
		$ctrl = new PageController();
		$template->assign( SLIDERBOX, $ctrl->viewProductImageSlider( $detail));
		$template->parse( PAGECONTENT . "." . DETAIL . "." . SLIDERBOX);
		
		$template->assign( DETAIL, $detail);
		$template->parse( PAGECONTENT . "." . DETAIL);
		
		// other tour
		/* $conArray= array(array(KEY => EQUAL, COLUMN => PRODUCT_CATEGORYID, VALUE =>
		 * $detail[PRODUCT_CATEGORYID]), SQL_AND, array(KEY => NOT_EQUAL, COLUMN => PRODUCT_ID,
		 * VALUE => $detail[PRODUCT_ID])); $list =
		 * $db->selectDifficultRecord($GLOBALS['productFullColArray'], PRODUCTS, $conArray,
		 * array(PRODUCT_UPDATETIME => DESC), 1, 6); foreach ( $list as $item ) {
		 * $item[PRODUCT_SHOWPRICE] = editPrice( $item[PRODUCT_SHOWPRICE],
		 * $item[PRODUCT_SHOWCURRENCY]); $item[PRODUCT_PRICE] = editPrice( $item[PRODUCT_PRICE],
		 * $item[PRODUCT_CURRENCY], 0); $item[PRODUCT_URL] = createURL( $this->lan, TOURS,
		 * $item[PRODUCT_URL]); $template->assign( ITEM, $item); $template->parse( PAGECONTENT . "."
		 * . OTHERTOUR . "." . ITEM); } $template->parse( PAGECONTENT . "." . OTHERTOUR); */
		
		$template->assign( PATHWAY, createPathWay( $detail[PRODUCT_CATEGORYID]));
		
		// return page
		$template->parse( PAGECONTENT);
		return array(WEBHEADER => $detail, PAGECONTENT => $template->out( PAGECONTENT));
	}

	/**
	 *
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewListArticle($module = ARTICLES, $cate = null, $type = CATEGORY_TYPE_ACRTICLE) {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/listarticle.html");
		$db = new DBAccess();
		
		if ($cate == null) {
			$list = $db->selectManyByCauseWithPaging( $GLOBALS['articleFullColArray'], $module, null, array(ARTICLE_UPDATETIME => DESC), getPageNumber(), 12);
			$template->assign( BOXTITLE, $GLOBALS['boxTitleArray'][$module]);
		} else {
			$conArrayVaule = array($cate[CATEGORY_ID]);
			$conArrayVaule = createSubIDArray( $cate[CATEGORY_ID], $conArrayVaule, $type);
			$conArray = array(array('key' => IN, 'column' => PRODUCT_CATEGORYID, 'value' => $conArrayVaule));
			$list = $db->selectDifficultRecord( $GLOBALS['articleFullColArray'], $module, $conArray, array(ARTICLE_UPDATETIME => DESC), 1, 10000);
		}
		
		foreach ( $list as $item ) {
			$item[ARTICLE_URL] = createURL( $this->lan, $module, $item[ARTICLE_URL]);
			$template->assign( ITEM, $item);
			$template->parse( PAGECONTENT . "." . ITEM);
		}
		
		$template->assign( PATHWAY, createPathWay( $cate[CATEGORY_ID]));
		
		// assign right banner
		$ctl = new PageController();
		$template->assign( RIGHTBANNER, $ctl->viewRightBanner());
		
		// return page
		$template->parse( PAGECONTENT);
		return array(WEBHEADER => $cate, PAGECONTENT => $template->out( PAGECONTENT));
	}

	/**
	 *
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewListFiles($module = FILES, $cate = null, $type = CATEGORY_TYPE_ACRTICLE) {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/listfiles.html");
		$db = new DBAccess();
		
		if ($_REQUEST[PARAM_URL] == "tai-ve") {
			
			$listCate = $db->selectManyByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_TYPE => CATEGORY_TYPE_COLLECTION), array(CATEGORY_POSITION => ASC));
			
			foreach ( $listCate as $cate ) {
				
				$list = $db->selectManyByCause( $GLOBALS['fileFullColArray'], FILES, array(FILE_CID => $cate[CATEGORY_ID]), array(FILE_ID => DESC));
				
				if (isset( $_SESSION[SESSION_USERNAME]) && ! empty( $_SESSION[SESSION_USERNAME])) {
					$i = 1;
					foreach ( $list as $item ) {
						$item[FILE_ID] = $i ++;
						$template->assign( ITEM, $item);
						$template->parse( PAGECONTENT . "." . CATEGORY . "." . ITEM);
					}
				} else {
					$i = 1;
					foreach ( $list as $item ) {
						$item[FILE_ID] = $i ++;
						$item[FILE_LINK] = "javascript:alert('Vui lòng đăng nhập để tải file.')";
						$template->assign( ITEM, $item);
						$template->parse( PAGECONTENT . "." . CATEGORY . "." . ITEM);
					}
				}
				
				$template->assign( CATEGORY, $cate);
				$template->parse( PAGECONTENT . "." . CATEGORY);
			}
			
			$template->assign( PATHWAY, "<ul class=\"path-way\">
		        							<li><a href=\"\"><span class=\"home-icon\"></span></a></li>
		        							<li><a href=\"vn/tai-ve.html\">Tải file</a></li>
		        						</ul>");
		
		} else {
			
			$cate = $db->selectOneByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_URL => $_REQUEST[PARAM_URL]));
			
			$list = $db->selectManyByCause( $GLOBALS['fileFullColArray'], FILES, array(FILE_CID => $cate[CATEGORY_ID]), array(FILE_ID => DESC));
			
			if (isset( $_SESSION[SESSION_USERNAME]) && ! empty( $_SESSION[SESSION_USERNAME])) {
				$i = 1;
				foreach ( $list as $item ) {
					$item[FILE_ID] = $i ++;
					$template->assign( ITEM, $item);
					$template->parse( PAGECONTENT . "." . CATEGORY . "." . ITEM);
				}
			} else {
				$i = 1;
				foreach ( $list as $item ) {
					$item[FILE_ID] = $i ++;
					$item[FILE_LINK] = "javascript:alert('Vui lòng đăng nhập để tải file.')";
					$template->assign( ITEM, $item);
					$template->parse( PAGECONTENT . "." . CATEGORY . "." . ITEM);
				}
			}
			
			$template->assign( CATEGORY, $cate);
			$template->parse( PAGECONTENT . "." . CATEGORY);
			
			$template->assign( PATHWAY, createPathWay( $cate[CATEGORY_ID]));
		
		}
		
		// return page
		$template->parse( PAGECONTENT);
		return array(WEBHEADER => $cate, PAGECONTENT => $template->out( PAGECONTENT));
	}

	/**
	 *
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewDetailArticle() {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/detailarticle.html");
		$db = new DBAccess();
		
		$detail = $db->selectOneByCause( $GLOBALS['articleFullColArray'], ARTICLES, array(ARTICLE_URL => $_REQUEST[PARAM_URL]));
		
		$detail[ARTICLE_URL] = createURL( $this->lan, ARTICLES, $detail[ARTICLE_URL]);
		$template->assign( ITEM, $detail);
		$template->parse( PAGECONTENT . "." . ITEM);
		
		// $template->assign( BOXTITLE, $detail[ARTICLE_TITLE]);
		$template->assign( PARAM_URL, $detail[ARTICLE_URL]);
		
		// other tour
		$conArray = array(array(KEY => EQUAL, COLUMN => ARTICLE_CATEGORYID, VALUE => $detail[ARTICLE_CATEGORYID]), SQL_AND, array(KEY => NOT_EQUAL, COLUMN => ARTICLE_ID, VALUE => $detail[ARTICLE_ID]));
		$list = $db->selectDifficultRecord( $GLOBALS['articleFullColArray'], ARTICLES, $conArray, array(ARTICLE_UPDATETIME => DESC), 1, 6);
		
		if (count( $list) > 0) {
			
			foreach ( $list as $item ) {
				$item[ARTICLE_URL] = createURL( $this->lan, ARTICLES, $item[ARTICLE_URL]);
				$template->assign( ITEM, $item);
				$template->parse( PAGECONTENT . "." . OTHERTOUR . "." . ITEM);
			}
			$template->parse( PAGECONTENT . "." . OTHERTOUR);
		}
		
		$template->assign( PATHWAY, createPathWay( $detail[ARTICLE_CATEGORYID]));
		
		// assign right banner
		$ctl = new PageController();
		$template->assign( RIGHTBANNER, $ctl->viewRightBanner());
		
		// return page
		$template->parse( PAGECONTENT);
		return array(WEBHEADER => $detail, PAGECONTENT => $template->out( PAGECONTENT));
	}

	/**
	 *
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewSearch() {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/search.html");
		
		// get keyword paramater
		$keyword = $_REQUEST["keyword"];
		$keyword = substr( $keyword, 0, strlen( $keyword) - 5);
		$template->assign( "Keword", $keyword);
		$count = 0;
		
		if ($keyword != null) {
			$decodeKeyword = encodeKeyword( $keyword);
			$db = new DBAccess();
			$list = $db->selectDifficultRecord( $GLOBALS['dataTypeFullColArray'], DATATYPE, array(array(COLUMN => DATATYPE_ITEMURL, KEY => LIKE, VALUE => $decodeKeyword)));
			
			foreach ( $list as $searchItem ) {
				switch ($searchItem[DATATYPE_TABLENAME]) {
					case ARTICLES :
						$item = $db->selectOneByCause( $GLOBALS['articleFullColArray'], ARTICLES, array(ARTICLE_ID => $searchItem[DATATYPE_ITEMID]));
						if ($item != null) {
							$item[ARTICLE_URL] = createURL( $this->lan, ARTICLES, $item[ARTICLE_URL]);
							$template->assign( ITEM, $item);
							$template->parse( PAGECONTENT . "." . ITEM);
							$count ++;
						}
						break;
					
					case PRODUCTS :
						$item = $db->selectOneByCause( $GLOBALS['productFullColArray'], PRODUCTS, array(PRODUCT_ID => $searchItem[DATATYPE_ID]));
						if ($item != null) {
							$item[PRODUCT_URL] = createURL( $this->lan, PRODUCTS, $item[PRODUCT_URL]);
							$template->assign( ITEM, $item);
							$template->parse( PAGECONTENT . "." . ITEM);
							$count ++;
						}
						break;
				}
			}
			if ($count == 0){
				//$template->assign( "NOITEM", $item);
				$template->parse( PAGECONTENT . ".NOITEM");
			}
		}
		
		// return page
		$template->parse( PAGECONTENT);
		return array(WEBHEADER => null, PAGECONTENT => $template->out( PAGECONTENT));
	}

	/**
	 */
	function __destruct() {
		
		// TODO - Insert your code here
	}
}

?>